﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain;
using Nop.Core.Domain.Blogs;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Localization;
using Nop.Core.Domain.Media;
using Nop.Services.Blogs;
using Nop.Services.Customers;
using Nop.Services.Helpers;
using Nop.Services.Localization;
using Nop.Services.Media;
using Nop.Services.Messages;
using Nop.Services.Seo;
using Nop.Web.Framework;
using Nop.Web.Framework.Controllers;
using Nop.Web.Models.Blogs;
using Taoke.Core;
using Taoke.Services;
using Top.Api.Domain;
using Web.Models.Taoke;
using Nop.Web.Extensions;

namespace Nop.Web.Controllers
{
    
    public class TaokeController : BaseNopController
    {
		#region Fields
        
        #endregion

		#region Constructors

        public TaokeController()
        {

        }

		#endregion

        #region Utilities

        #endregion

        #region Methods
        public ActionResult ItemList(SearchModel seachModel,SearchPagingFilteringModel command)
        {
            var model = new TaokeItemListModel();
            if (command.PageSize <= 0) command.PageSize = 33;
            if (command.PageNumber <= 0) command.PageNumber = 0;
            if (seachModel.CategoryId == 0&&string.IsNullOrEmpty(seachModel.Keyword)) seachModel.CategoryId = TOPHelper.Categry_Women;
            var items = TaoBaoKeService.GetInstance.GetItems(seachModel.CategoryId, seachModel.Keyword, seachModel.Sort, seachModel.StartPrice, seachModel.EndPrice, command.PageNumber, command.PageSize);
            model.PagingFilteringContext.LoadPagedList(items);
            foreach (TaobaokeItem item in items)
            {
                TaokeItemModel taokeItemModel = new TaokeItemModel();
                taokeItemModel.NumIid = item.NumIid;
                taokeItemModel.ClickUrl = item.ClickUrl;
                taokeItemModel.PicUrl = item.PicUrl;
                taokeItemModel.Price = item.Price;
                taokeItemModel.CouponPrice = item.CouponPrice;
                taokeItemModel.CouponStartTime = item.CouponStartTime;
                taokeItemModel.CouponEndTime = item.CouponEndTime;
                taokeItemModel.Title = item.Title.RemoveHtml().FilterDangerousString();
                taokeItemModel.Volume = item.Volume;
                model.TaokeItems.Add(taokeItemModel);
            }
            ItemCat category = new ItemCat();
            string categoryName = string.Empty;
            if (seachModel.CategoryId > 0)
            {
                category=TaoBaoKeService.GetInstance.GetItemCat(seachModel.CategoryId.ToString());
            }
            if (category != null)
            {
                categoryName = category.Name;
            }
            ViewBag.CustomTitle =Server.UrlDecode( seachModel.Keyword) + " - " + "第" + (command.PageNumber + 1) + "页";
            if (!string.IsNullOrEmpty(categoryName))
            {
                ViewBag.CustomTitle =Server.UrlDecode( seachModel.Keyword) + " - " + categoryName + "-第" + (command.PageNumber + 1) + "页";
            }
            return View(model);
        }
        public ActionResult ItemDetail(string NumIid,long volume)
        {
            var taoBaoItemDetail = TaoBaoKeService.GetInstance.GetTaokeItemDetail(NumIid);
            //prepare category name
            ItemCat cat = TaoBaoKeService.GetInstance.GetItemCat(taoBaoItemDetail.Item.Cid.ToString());

            //prepare TaokeItemModel
            var taokeItemModel = new TaokeItemModel();
            taokeItemModel.ClickUrl = taoBaoItemDetail.ClickUrl;
            taokeItemModel.ShopClickUrl = taoBaoItemDetail.ShopClickUrl;
            taokeItemModel.NumIid = taoBaoItemDetail.Item.NumIid;
            taokeItemModel.ItemModel.PicUrl = taoBaoItemDetail.Item.PicUrl;
            taokeItemModel.ItemModel.Price = taoBaoItemDetail.Item.Price;
            taokeItemModel.ItemModel.ListTime = taoBaoItemDetail.Item.ListTime;
            taokeItemModel.ItemModel.DelistTime = taoBaoItemDetail.Item.DelistTime;
            taokeItemModel.ItemModel.Desc = Regex.Replace(taoBaoItemDetail.Item.Desc, "<a.*</a>", "").Replace("<br><br><br><br><br><br><br>", "");
            taokeItemModel.ItemModel.FreightPayer = taoBaoItemDetail.Item.FreightPayer;
            taokeItemModel.ItemModel.Title = taoBaoItemDetail.Item.Title;
            taokeItemModel.Title = taoBaoItemDetail.Item.Title;
            taokeItemModel.ItemModel.Num = taoBaoItemDetail.Item.Num;
            taokeItemModel.ItemModel.PostFee = taoBaoItemDetail.Item.PostFee;
            taokeItemModel.ItemModel.ExpressFee = taoBaoItemDetail.Item.ExpressFee;
            taokeItemModel.ItemModel.EmsFee = taoBaoItemDetail.Item.EmsFee;
            taokeItemModel.SellerCreditScore = taoBaoItemDetail.SellerCreditScore;
            taokeItemModel.ItemModel.Volume = volume;
            taokeItemModel.ItemModel.Cid = taoBaoItemDetail.Item.Cid;
            taokeItemModel.ItemModel.CategoryName = cat.Name;
            taokeItemModel.ItemModel.InputStr = taoBaoItemDetail.Item.InputStr;
            taokeItemModel.ItemModel.PropsName = taoBaoItemDetail.Item.PropsName;
            taokeItemModel.ItemModel.WapDesc = taoBaoItemDetail.Item.WapDesc;
            taokeItemModel.ItemModel.WapDetailUrl = taoBaoItemDetail.Item.WapDetailUrl;
            taokeItemModel.PicUrl= taoBaoItemDetail.Item.PicUrl;

            return View(taokeItemModel) ;
        }
        public ActionResult RecommentList(SearchModel seachModel)
        {
            var model = new TaokeItemListModel();
            string sort = "commissionNum_desc";
            if (seachModel.CategoryId == 0) seachModel.CategoryId = TOPHelper.Categry_Women;
            var items = TaoBaoKeService.GetInstance.GetItems(seachModel.CategoryId, seachModel.Keyword, sort, seachModel.StartPrice, seachModel.EndPrice, 1, 13);
            foreach (TaobaokeItem item in items)
            {
                TaokeItemModel taokeItemModel = new TaokeItemModel();
                taokeItemModel.NumIid = item.NumIid;
                taokeItemModel.ClickUrl = item.ClickUrl;
                taokeItemModel.PicUrl = item.PicUrl;
                taokeItemModel.Price = item.Price;
                taokeItemModel.CouponPrice = item.CouponPrice;
                taokeItemModel.CouponStartTime = item.CouponStartTime;
                taokeItemModel.CouponEndTime = item.CouponEndTime;
                taokeItemModel.Title = item.Title.RemoveHtml().FilterDangerousString();
                taokeItemModel.Volume = item.Volume;
                model.TaokeItems.Add(taokeItemModel);
            }
            return View(model);
        }
        public ActionResult Search (SearchModel searchModel, SearchPagingFilteringModel command)
        {
            if (searchModel == null)
                searchModel = new SearchModel();
            if (command.PageSize <= 0) command.PageSize = 10;
            if (command.PageNumber <= 0) command.PageNumber = 0;

            var model = new TaokeItemListModel();
            string sort = "commissionNum_desc";
            if (searchModel.CategoryId == 0) searchModel.CategoryId = TOPHelper.Categry_Women;
            var items = TaoBaoKeService.GetInstance.GetItems(searchModel.CategoryId, searchModel.Keyword, sort, searchModel.StartPrice, searchModel.EndPrice, command.PageNumber , command.PageSize);
            searchModel.PagingFilteringContext.LoadPagedList(items);
            foreach (TaobaokeItem item in items)
            {
                TaokeItemModel taokeItemModel = new TaokeItemModel();
                taokeItemModel.NumIid = item.NumIid;
                taokeItemModel.ClickUrl = item.ClickUrl;
                taokeItemModel.PicUrl = item.PicUrl;
                taokeItemModel.Price = item.Price;
                taokeItemModel.CouponPrice = item.CouponPrice;
                taokeItemModel.CouponStartTime = item.CouponStartTime;
                taokeItemModel.CouponEndTime = item.CouponEndTime;
                taokeItemModel.Title = item.Title.RemoveHtml().FilterDangerousString();
                taokeItemModel.Volume = item.Volume;
                model.TaokeItems.Add(taokeItemModel);
            }
            searchModel.TaokeItems = model.TaokeItems;
            return View(searchModel);
        }
        public ActionResult HotList(long categoryId)
        {
            var model = new TaokeItemListModel();
            string keyword = null;
            string sort = " commissionVolume_desc";
            string startPrice = null;
            string endPrice = null;
            var items = TaoBaoKeService.GetInstance.GetItems(categoryId, keyword, sort, startPrice, endPrice, 0, 8);
            foreach (TaobaokeItem item in items)
            {
                TaokeItemModel taokeItemModel = new TaokeItemModel();
                taokeItemModel.NumIid = item.NumIid;
                taokeItemModel.ClickUrl = item.ClickUrl;
                taokeItemModel.PicUrl = item.PicUrl;
                taokeItemModel.Price = item.Price;
                taokeItemModel.CouponPrice = item.CouponPrice;
                taokeItemModel.CouponStartTime = item.CouponStartTime;
                taokeItemModel.CouponEndTime = item.CouponEndTime;
                taokeItemModel.Title = item.Title.RemoveHtml().FilterDangerousString();
                taokeItemModel.Volume = item.Volume;
                model.TaokeItems.Add(taokeItemModel);
            }
            return View(model);
        }
        public ActionResult CouponList(long categoryId)
        {
            var model = new TaokeItemListModel();
            string keyword = null;
            string sort = " commissionVolume_desc";
            var items = TaoBaoKeService.GetInstance.GetCouponItems(categoryId, keyword, sort, 0, 8);
            foreach (TaobaokeItem item in items)
            {
                TaokeItemModel taokeItemModel = new TaokeItemModel();
                taokeItemModel.NumIid = item.NumIid;
                taokeItemModel.ClickUrl = item.ClickUrl;
                taokeItemModel.PicUrl = item.PicUrl;
                taokeItemModel.Price = item.Price;
                taokeItemModel.CouponPrice = item.CouponPrice;
                taokeItemModel.CouponStartTime = item.CouponStartTime;
                taokeItemModel.CouponEndTime = item.CouponEndTime;
                taokeItemModel.Title = item.Title.RemoveHtml().FilterDangerousString();
                taokeItemModel.Volume = item.Volume;
                model.TaokeItems.Add(taokeItemModel);
            }
            return View(model);
        }
        #endregion
    }

    public static class StringExtensions
    {
        public static string FilterDangerousString (this string source)
        {
            return source.Replace("%", "").Replace("/", "|").Replace("+", "").Replace("*", "").Replace("@", "")
                .Replace("#", "").Replace("$", "").Replace("^", "").Replace("&", "").Replace("(", "").Replace(")", "")
                .Replace("~", "").Replace("!", "").Replace("'", "").Replace("\"", "").Replace(";", "").Replace(":", "").Replace(",", ".")
                .Replace("{", "").Replace("}", "");
        }
    }
}
